package com.hc.api.controller;

import com.hc.api.utils.ApiUtil;
import com.hc.api.utils.ShareConfigUtils;
import com.hc.comm.base.BaseController;
import com.hc.comm.dto.UserStepNumberDTO;
import com.hc.comm.utils.DateUtil;
import com.hc.comm.utils.JsonUtils;
import com.hc.comm.vo.UserStepNumberVO;
import com.hc.service.model.UserStepNumberModel;
import com.hc.service.service.IUserStepNumberService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.hc.comm.utils.JsonUtils.UTF8;

/**
 * 微信小程序测试controller
 * Created by yx on 2017/9/5.
 */
@Controller
@Scope("prototype")
@RequestMapping("/" + ApiUtil.ApiVersion.V1 + "/wxsmall/")
public class WxSmallProController extends BaseController {

    @Resource(name = "userStepNumberService")
    private IUserStepNumberService userStepNumberService;

    //https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
    @RequestMapping("getOpen")
    public void getOpenId() throws Exception {
        String code = request.getParameter("code");
        System.out.println("code:" + code);
        String sessionKey = ShareConfigUtils.getWXSmallSessionKey(code);
        System.out.println("sessionKey:" + sessionKey);
        //writeToJson(sessionKey);

        response.setCharacterEncoding(UTF8);
        response.setHeader("Cache-Control", "no-cache");
        response.getWriter().write(sessionKey);
    }

    /**
     * 获取步数列表
     * 参数为 筛选条件 微信用户
     *
     * @throws Exception
     */
    @RequestMapping("getStep")
    public void getStep() throws Exception {
        System.out.println("today:" + DateUtil.getToDayStartTime());
        System.out.println("afterday:" + DateUtil.getAfterDayStartTime());
        List<UserStepNumberModel> UserStepNumberModelList =
                userStepNumberService.getTodayStepNum(DateUtil.getToDayStartTime(), DateUtil.getAfterDayStartTime());
        ArrayList<UserStepNumberVO> UserStepNumberVoList = new ArrayList<>();
        int sort = 1;
        for (UserStepNumberModel userStepNumberModel : UserStepNumberModelList) {
            UserStepNumberVO vo = new UserStepNumberVO();
            vo.copyPropertis(userStepNumberModel);
            vo.setSort(sort);
            UserStepNumberVoList.add(vo);
            sort++;
        }

        JsonUtils.toSuccessResult(request, response, UserStepNumberVoList,
                "sort", "stepNumber", "stepTime", "wxImage", "wxName");

    }

    public static void main(String[] args) throws Exception {

    }

}
